import Author from '@/components/Author'
import { getMyPrize, deletePrize } from '@/api/prize'
import Pagination from '@/components/Pagination'
// 权限判断指令
import { getFaculty } from '@/api/account'
import permission from '@/directive/permission/index.js'
import ElImageViewer from 'element-ui/packages/image/src/image-viewer'

const defaultListQuery = {
  page: 1,
  limit: 10,
  ordering: '-id',
  title: ''
}

export default {
  name: 'PrizeList',
  components: { Pagination, Author, ElImageViewer },
  directives: { permission },
  filters: {
    statusFilter(status) {
      const statusMap = {
        '未审核': 'info',
        '通过': 'success',
        '不通过': 'danger'
      }
      return statusMap[status]
    },
    textFilter(value) {
      const textMap = {
        false: '否',
        true: '是'
      }
      return textMap[value]
    }
  },
  data() {
    return {
      detailDialogVisible: false,
      showViewer: false,
      fileList: [],
      tableKey: 0,
      facultyList:[],
      list: [],
      total: 0,
      listLoading: true,
      // 查询条件
      listQuery: Object.assign({}, defaultListQuery),
      formData: {},
    }
  },
  activated() {
    // 新增成功后跳转到该页，刷新数据
    const refresh = this.$route.params.refresh
    if (refresh) {
      this.init()
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      this.getList()
      this.getFaculty()
    },
    getFaculty(){
      getFaculty().then(res=>{
        this.facultyList=res
      })
    },
    // 获取表格数据
    getList() {
      // const baseUrl = process.env.VUE_APP_BASE_API
      this.listLoading = true
      getMyPrize(this.listQuery).then(response => {
        this.list = response.results
        // this.list = response.results.map((item) => {
        //   var newImage = item.images.map((i) => {
        //     return baseUrl + i
        //   })
        //   item.images = newImage
        //   return item
        // })
        this.total = response.count

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 0.5 * 1000)
      })
    },

    // 搜索事件
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },

    // 重置搜索条件
    resetFilter() {
      this.listQuery = Object.assign({}, defaultListQuery)
    },

    // 点击删除事件
    handleDelete(row, index) {
      this.$confirm('此操作将删除该获奖, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deletePrize(row.id).then(response => {
          this.list.splice(index, 1)
          this.$notify({
            title: '删除成功',
            type: 'success',
            message: '成功删除获奖 ' + row.title
          })
        })
      }).catch((res) => {
        this.$notify.error({
          title: '失败',
          message: res.response.data.message,
          duration: 2000
        })
      })
    },
    // 点击修改
    showUpdateDialog(row) {
      this.$router.push({
        name: 'PrizeUpdate',
        query: { from: 'BookList',id: row.id }
      })
    },

    // 点击新增事件
    showCreateDialog() {
      this.$router.push({ path: 'add_prize' })
    },

    // 点击查看事件
    handleView(row) {
      this.formData = row
      this.authorData = row.authors
      this.nowImages = row.images
      this.detailDialogVisible = !this.detailDialogVisible
      var fileList = []
      for (var i = 0; i < row.images.length; ++i) {
        fileList.push({ name: '111', url: row.images[i] })
      }
      this.fileList = fileList
    },
    open() {
      setTimeout(() => {
        if (!this.add) {
          this.addBaseFormCss()
        }
      }, 100)
    },

    // 为基本信息表单添加css
    addBaseFormCss() {
      const form1 = document.getElementById('form1')
      if (!form1) {
        return
      }
      const tmp = form1.childNodes[0].childNodes[0].childNodes[0].childNodes[0]
      const count = tmp.childElementCount
      for (var i = 0; i < count; i++) {
        if (i === count - 1) {
          var className = tmp.childNodes[i].getAttribute('class') + ' last-node'
        } else {
          className = tmp.childNodes[i].getAttribute('class') + ' around-border'
        }
        tmp.childNodes[i].setAttribute('class', className)
      }
      this.add = true // 用来控制只添加一次css
    },

    // 图片预览
    handlePictureCardPreview(file) {
      this.imgIndex = this.nowImages.indexOf(file.url)
      this.showViewer = true
    },
    closeViewer() {
      this.showViewer = false
    },
    toDetail(id) {
      this.$router.push({
        name: 'PrizeDetail',
        query: { from: 'MyPrice',id: id }
      })
    }

  }
}
